package com.api.workflow.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.util.string.StringUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Cell;
import weaver.soa.workflow.request.DetailTable;
import weaver.soa.workflow.request.RequestInfo;
import weaver.soa.workflow.request.Row;
/**
 * @author wanghan
 * @version 1.0
 * @Description
 * @since 2025/2/27 12:29
 */
public class PRVerifyAction extends BaseBean implements Action{
    public static void main(String[] args) {

    }
    @Override
    public String execute(RequestInfo requestInfo) {
        try {
            //获得流程id
            String workflowid = requestInfo.getWorkflowid();
            //获取表id
            String requestid = requestInfo.getRequestid();
            writeLog("PRVerifyAction-----start----------------------" + requestid);
            RecordSet rs = new RecordSet();
            rs.execute("select tablename from workflow_base a,workflow_bill b where a.formid=b.id and a.id=" + workflowid);
            rs.next();
            //获取主表名
            String maintable = Util.null2String(rs.getString("tablename"));
            writeLog("maintablemaintablemaintable" + maintable);
            DetailTable[] detailTable = requestInfo.getDetailTableInfo().getDetailTable();
            writeLog("detailTabledetailTable" + detailTable);
            JSONArray arrayListmx1 = new JSONArray();
            DetailTable detailTablemx1=detailTable[0];
            Row[] rowmx1 = detailTablemx1.getRow();
            if (rowmx1.length>0){
                for (Row r:rowmx1){//遍历明细1
                    JSONObject jsonObject = new JSONObject();
                    Cell[] cell = r.getCell();
                    for (Cell c1:cell){
                        jsonObject.put(c1.getName(),c1.getValue());
                    }
                    arrayListmx1.add(jsonObject);

                }
            }
            writeLog("arrayListmx1" + arrayListmx1);
            if (arrayListmx1.size()>0){
                for (int i=0;i<arrayListmx1.size();i++){
                    String sql="";
                    String cgbs = String.valueOf(arrayListmx1.getJSONObject(i).get("cgbs"));//PR单id

                    writeLog("cgbs" + cgbs);
                    if(!StringUtil.isNullOrEmpty(cgbs)){
                        Double cgsl = Double.valueOf(String.valueOf(arrayListmx1.getJSONObject(i).get("cgsl")));//当前采购数量
                        writeLog("cgsl" + cgsl);
                        Double sysysl = Double.valueOf(String.valueOf(arrayListmx1.getJSONObject(i).get("sysysl")));//剩余使用数量
                        writeLog("sysysl" + sysysl);
                        sql="select cgsqbh,cgsqxxm from uf_cgsqd where id=?";
                        writeLog("sql-----------" + sql);
                        rs.executeQuery(sql,cgbs);
                        rs.next();
                        String cgsqbh = Util.null2String(rs.getString("cgsqbh"));//采购申请编号
                        String cgsqxxm = Util.null2String(rs.getString("cgsqxxm"));//采购申请行项目号

                        if(sysysl<cgsl){
                            writeLog("sssssss");
                            String message="采购申请编号为："+cgsqbh+" 采购申请行项目为："+cgsqxxm+" 的PR单,"+ "当前申请数量为："+cgsl+" 剩余申请数量为："+sysysl+" 申请数量已经超过需求数量，请您调整申请数量！";
                            requestInfo.getRequestManager().setMessagecontent(message);
                            return Action.FAILURE_AND_CONTINUE;
                        }else {
                            writeLog("eeeeeeee");
                        }
                    }

                }
            }
            return Action.SUCCESS;

        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessagecontent("接口出错,请联系管理员!"+e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
